﻿<?php

//就近 www.jiujin.me
if (!defined('IN_IA')) {
	die('Access Denied');
}
global $_W, $_GPC;
$id = $_GPC['id'];
$dist = $_GPC['dist'];
$endtime = $_GPC['endtime'];
$downsn = $_GPC['downsn'];
$openid = m('user')->getOpenid();
$member = m('member')->getInfo($openid);
$mappingfans = pdo_fetch("SELECT * FROM " . tablename('mc_mapping_fans') . " WHERE openid = '{$openid}' and follow = 1 and uniacid = '{$_W['uniacid']}'");
if(empty($mappingfans)){
	include $this->template('package/sub');
	die;
}
//套盒二维码 160316
$time = time();
$package = pdo_fetch("SELECT * FROM " . tablename('ewei_shop_package') . " WHERE id = '{$id}' and uniacid = '{$_W['uniacid']}' and status = 1");

if(!empty($package)){
	//会员组限制
	//$find_mc = pdo_fetch("SELECT groupid FROM " . tablename('mc_members') . " WHERE uid = '{$_W['member']['uid']}' and uniacid = '{$_W['uniacid']}'");
	$grouparr = explode(',',$package['group']);
	if(strlen($package['group'])>0 && !empty($grouparr)){
		if(!in_array($member['groupid'],$grouparr)){
			include $this->template('package/group');
			die;
		}
	}
	//时间限制
	if($time >= $endtime){
		header("location: http://wx.lanxuevip.com/app/index.php?i=5&c=entry&do=used&m=ewei_shop");
		die;
	}
	//扫过的次数
	$all_count = pdo_fetch("SELECT count(*) as count FROM " . tablename('ewei_shop_package_record') . " WHERE package = '{$id}' and uniacid = '{$_W['uniacid']}' and dist = '{$dist}' and downsn = '{$downsn}'");
         
         if(intval($all_count['count']) < 1 || empty($all_count)){
		$insert = array(
			'uniacid' => $_W['uniacid'],
			'package' => $id,
			'createtime' => time(),
			'uid' => $member['uid'],
			'nickname' => $member['nickname'],
			'goods' => $package['goods'],
			'type' => $package['type'],
			'money' => $package['money'],
			'dist' => $dist,
			'downsn' => $downsn
		);
		pdo_insert('ewei_shop_package_record', $insert);
		
		//增加余额充值明细记录
		if($package['type'] == 2){
			$logno = m('common')->createNO('member_log', 'logno', 'RC');
			$charge = array('openid' => $member['openid'], 'uniacid' => $_W['uniacid'], 'logno' => $logno, 'type' => '0', 'createtime' => time(), 'status' => '1', 'title' => $set['name'] . '套盒二维码充值', 'money' => $package['money'], 'rechargetype' => 'package');
			pdo_insert('ewei_shop_member_log', $charge);
		}
		/*
		//增加推送消息
		$find_url = 'http://wx.lanxuevip.com/app/index.php?i=5&c=entry&do=package&m=ewei_shop&id='.$id;
		$news_reply = pdo_fetchall("SELECT * FROM " . tablename('news_reply') . " WHERE url = '{$find_url}' order by id desc limit 1");
		if(!empty($news_reply)){
			$fans = pdo_fetch('SELECT salt,acid,openid FROM ' . tablename('mc_mapping_fans') . ' WHERE acid = :acid AND openid = :openid', array(':acid' => $_W['acid'], ':openid' => $member['openid']));
			$rule = pdo_fetch('SELECT module,name FROM ' . tablename('rule') . ' WHERE id = :rid', array(':rid' => $news_reply['rid']));
			$idata = array('rid' => $rid, 'name' => $rule['name'], 'module' => $rule['module']);
			$module = $rule['module'];
			foreach($news_reply as $c) {
				$row = array();
				$row['title'] = urlencode($c['title']);
				$row['description'] = urlencode($c['description']);
				!empty($c['thumb']) && ($row['picurl'] = tomedia($c['thumb']));
			
				if(strexists($c['url'], 'http://') || strexists($c['url'], 'https://')) {
					$row['url'] = $c['url'];
				} else {
					$pass['time'] = TIMESTAMP;
					$pass['acid'] = $fans['acid'];
					$pass['openid'] = $member['openid'];
					$pass['hash'] = md5("{$member['openid']}{$pass['time']}{$fans['salt']}{$_W['config']['setting']['authkey']}");
					$auth = base64_encode(json_encode($pass));
					$vars = array();
					$vars['__auth'] = $auth;
					$vars['forward'] = base64_encode($c['url']);
					$row['url'] =  $_W['siteroot'] . 'app/' . murl('auth/forward', $vars);
				}
				$news[] = $row;
			}
			$send['news']['articles'] = $news;
			$send['touser'] = $member['openid'];
			$send['msgtype'] = 'news';
			$acid = $_W['acid'];
			if($acid) {
				$acc = WeAccount::create($acid);
				$data = $acc->sendCustomNotice($send);
			}
		}
		*/
		if($package['type'] == 2){
			//修改余额
			$credit2 = intval($member['credit2']) + intval($package['money']);
			$data = array('credit2' => $credit2);
			if($member['uid'] > 0){
				pdo_update('mc_members', $data, array('uniacid' => $_W['uniacid'], 'uid' => $member['uid']));
			}
		}
	}else{
		header("location: http://wx.lanxuevip.com/app/index.php?i=5&c=entry&do=used&m=ewei_shop");
		die;
	}
}
include $this->template('package/list');

